require("luaext")



local moveUp = "up"
local moveDown = "down"
local moveLeft = "left"
local moveRight = "right"


--格子是否有解
--返回解的坐标， 与动作方向
local function isDie(maps, x, y) 
	local row = #maps[1]+1
	local col = #maps+1
	local lx1 = x - 1 > 0;
	local lx2 = x - 2 > 0;
	local lx3 = x - 3 > 0;
	local bx1 = x + 1 < row;
	local bx2 = x + 2 < row;
	local bx3 = x + 3 < row;
	local ly1 = y - 1 > 0;
	local ly2 = y - 2 > 0;
	local ly3 = y - 3 > 0;
	local by1 = y + 1 < col;
	local by2 = y + 2 < col;
	local by3 = y + 3 < col;
	local color = maps[y][x];
	if color==0 then 
		return true
	end
	local function isMapValue(x,y)
		if maps[y] and maps[y][x] and maps[y][x]>0 then
			return true
		end
		return false
	end	
	if (bx1) then
		
		if (maps[y][x + 1] == color) then --相同的在右边
			if (bx3) then
				if (maps[y][x + 3] == color) and isMapValue(x+2,y) then							
					return false, x+3, y, moveLeft
				end
			end
			if (bx2 and by1) then
				if (maps[y + 1][x + 2] == color) and isMapValue(x+2,y+2)  then					
					return false, x+2, y+1, moveUp
				end
			end
			if (bx2 and ly1) then
				if (maps[y - 1][x + 2] == color) and isMapValue(x+2,y-2)  then					
					return false, x+2, y-1, moveDown
				end
			end
			if (lx2) then
				if (maps[y][x - 2] == color) and isMapValue(x-1,y)  then					
					return false, x-2, y, moveRight
				end
			end
			if (lx1 and ly1) then
				if (maps[y - 1][x - 1] == color) and isMapValue(x-1,y-2)  then		
					return false, x-1, y-1, moveDown
				end
			end
			if (lx1 and by1) then
				if (maps[y + 1][x - 1] == color) and isMapValue(x-1,y)  then					
					return false, x-1, y+1, moveUp
				end
			end
		end
		if (ly1 and by1) then
			if (maps[y - 1][x + 1] == color and maps[y + 1][x + 1] == color) then				
				return false, x+1, y, moveLeft
			end
		end
	end
	if (lx1) then
		if (maps[y][x - 1] == color) then --相同的在左边
			if (lx3) then
				if (maps[y][x - 3] == color) and isMapValue(x-2,y) then
					return false, x-3, y, moveRight
				end
			end
			if (lx2 and by1) then
				if (maps[y + 1][x - 2] == color) and isMapValue(x-2,y)  then
					return false, x-2, y+1, moveUp
				end
			end
			if (lx2 and ly1) then
				if (maps[y - 1][x - 2] == color) and isMapValue(x-2,y)  then
					return false, x-2, y-1, moveDown
				end
			end
			if (bx2) then
				if (maps[y][x + 2] == color) and isMapValue(x+3,y)  then
					return false, x+2, y, moveRight
				end
			end
			if (bx1 and ly1) then
				if (maps[y - 1][x + 1] == color) and isMapValue(x+1,y)  then
					return false, x+1, y-1, moveDown
				end
			end
			if (bx1 and by1) then
				if (maps[y + 1][x + 1] == color) and isMapValue(x+1,y)  then
					return false, x+1, y+1, moveUp
				end
			end
		end
		if (ly1 and by1) then
			if (maps[y - 1][x - 1] == color and maps[y + 1][x - 1] == color) then
				return false, x-1, y, moveRight 
			end
		end
	end
	if (by1) then
		if (maps[y + 1][x] == color) then --相同的在下边
			if (by3) then
				if (maps[y + 3][x] == color) and isMapValue(x,y+2)  then
					return false, x, y+3, moveUp
				end
			end
			if (lx1 and by2) then
				if (maps[y + 2][x - 1] == color) and isMapValue(x,y+2)  then
					return false, x-1, y+2, moveRight
				end
			end
			if (bx1 and by2) then
				if (maps[y + 2][x + 1] == color) and isMapValue(x,y+2)  then
					return false, x+1, y+2, moveLeft
				end
			end
			if (ly2) then
				if (maps[y - 2][x] == color) and isMapValue(x,y-1)  then
					return false, x, y-2, moveUp
				end
			end
			if (bx1 and ly1) then
				if (maps[y - 1][x + 1] == color) and isMapValue(x,y-1)  then
					return false, x+1, y-1, moveLeft
				end
			end
			if (lx1 and ly1) then
				if (maps[y - 1][x - 1] == color) and isMapValue(x,y-1)  then
					return false, x-1, y-1, moveRight
				end
			end
		end
		if (lx1 and bx1) then --在第一行
			if (maps[y + 1][x - 1] == color and maps[y + 1][x + 1] == color) then
				return false, x, y+1, moveUp
			end
		end
	end
	if (ly1) then
		if (maps[y - 1][x] == color) then --相同的在上边
			if (ly3) then
				if (maps[y - 3][x] == color) and isMapValue(x,y-2)  then
					return false, x, y-3, moveDown
				end
			end
			if (lx1 and ly2) then
				if (maps[y - 2][x - 1] == color) and isMapValue(x,y-2)  then
					return false, x-1, y-2, moveRight
				end
			end
			if (bx1 and ly2) then
				if (maps[y - 2][x + 1] == color) and isMapValue(x,y-2)  then
					return false, x+1, y-2, moveLeft
				end
			end
			if (by2) then
				if (maps[y + 2][x] == color) and isMapValue(x,y+1)  then
					return false, x, y+2, moveUp
				end
			end
			if (bx1 and by1) then
				if (maps[y + 1][x + 1] == color) and isMapValue(x,y+1)  then
					return false, x+1, y+1, moveLeft
				end
			end
			if (lx1 and by1) then
				if (maps[y + 1][x - 1] == color) and isMapValue(x,y+1)  then
					return false, x-1, y+1, moveRight
				end
			end
		end
		if (lx1 and bx1) then --在底行
			if (maps[y - 1][x - 1] == color and maps[y - 1][x + 1] == color) then
				return false, x, y-1, moveDown
			end
		end
	end
	return true;
end

local maps = {
	{4,2,4,0,3,4,0},
	{1,3,5,2,1,2,2},
	{0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0},
	}
local str,x, y, action = isDie(maps, 6, 2)

print(str, x, y, action)







require("TSLib")




function click(x, y)
    touchDown(x, y)
    mSleep(30)
    touchMove(x, y) -- 安卓部分机型单点不生效的情况下可以加上此句
    touchUp(x, y)
end
--UINew()
--UIImage("../res/1.png") --创建一个图片框，默认居中对齐，图片不缩放，宽度默认一行，下一控件不换行
----可选参数如果写部分的话，该参数前的所有参数都必须需要填写，否则会报错
--UIShow()
--狐狸
local tbColor1 = {
	{   19,  466, 0xf7987a},
	{   27,  500, 0xee8779},
	{   34,  492, 0xde3920},
}
--青蛙
local tbColor2 = {
	{  139,  286, 0x014000},
	{  152,  292, 0x6ee525},
	{  152,  310, 0x2cab21},
	{  170,  303, 0x118b25},
}
--小鸡
local tbColor3 ={
	{   84,  306, 0xa75c0c},
	{   92,  311, 0xde5709},
	{   75,  317, 0xe5a43b},
	{   92,  297, 0xf9f730},
}
--河马
local tbColor4 = {
	{  225,  275, 0xb6d6f9},
	{  249,  255, 0x21bee2},
	{  236,  290, 0x0a52b6},
	{  255,  276, 0xb2d3f9},
}
--猫头鹰
local tbColor5 = {
	{  195,  357, 0x8c58bf},
	
	
}
--熊
local tbColor6 = {
	{  162,  334, 0x923c05},
	{  162,  324, 0xad5412},
	{  200,  321, 0xbb5f16},
	{  198,  336, 0x9f490c},
	{  179,  348, 0x833000},
}
local function makeMap()
	local maps = {}
	local wCell = 60
	local x0 = 25
	local y0 = 210

	for y=1, 7 do 
		local row = {}
		for x=1, 7 do 
			local sx = x*wCell
			local sy = y0+y*wCell 			
			local fx,fy = findMultiColorInRegionFuzzyByTable(tbColor1, sx, sy, (x+1)*wCell, y0+(y+1)*wCell, 1)
			--local fx,fy = findColorInRegionFuzzy(0xdf351c, 90, sx, sy, (x+1)*wCell, y0+(y+1)*wCell)
			local cx = sx+wCell/2
			local cy = sy+wCell/2
			--tap(cx, cy, 1000, "../res/1.png")
			--local str = ""..cx..","..cy
			--dialog(str, 0)
			--mSleep(1000)
			if fx~=-1 and fy~=-1 then 
				tap(cx, cy, 1000, "2.png")
				table.insert(row, 1)
			else
				table.insert(row, 0)
			end
		end
		table.insert(maps, row)
	end
	local str = dump(maps)
	
	dialog(str, 0)
end
init(0)
--tap(68, 340, 10000, "2.png")
 
local beginColor = {
	{  149,  238, 0x175890},
	{  149,  239, 0x0f3665},
	{  148,  239, 0x1965a0},
	{  148,  238, 0x228ed4},
}
--local fx,fy = findColorInRegionFuzzy(0x175890, 95, 60, 100, 360, 270)
local fx,fy = findMultiColorInRegionFuzzyByTable(beginColor,90, 60, 100, 360, 270)
local str = ""..fx..","..fy
--dialog(str, 0)
local beginX = fx
local beginY = fy
--makeMap()

local function findMultiColorInRegionFuzzyByTableQin(colors, coordinates)
	local fx,fy
	for k, color in pairs(colors) do 
		fx,fy = findColorInRegionFuzzy(color[3], 95, coordinates[1],coordinates[2],coordinates[3],coordinates[4])
		if fx==-1 or fy==-1 then 
			return -1,-1
		end
	end
	return  fx,fy
end

local tbStartXY = {
	{60,270},
	{148,238},
	{60,270},
	{0,270},
}
local startXY = tbStartXY[4]
local x0 = startXY[1]-60
local y0 = startXY[2]-60
local coordinateMaps = {}
local maps = {}

local function clear()

	keepScreen(true);   --打开保持屏幕，当参数为 true 时启用屏幕保持，运行到该函数时内存中的屏幕图像保持在这一时刻
	for y = 1, 8 do
		local row = {}
		table.insert(maps, row)
		table.insert(coordinateMaps, {})
		for x = 1, 9 do    
			maps[y][x] = 0
			local sx = x0+x*60
			local sy = y0+y*60
			coordinateMaps[y][x] = {sx+30, sy+30}
			local fx,fy = findMultiColorInRegionFuzzyByTableQin(tbColor1, {sx, sy, sx+60, sy+60} )
			--local fx,fy = findMultiColorInRegionFuzzyByTable(tbColor1,90, sx, sy, sx+60, sy+60)
			if fx~=-1 and fy~=-1 then 
				maps[y][x] = 1
			end		
			local fx,fy = findMultiColorInRegionFuzzyByTableQin(tbColor2, {sx, sy, sx+60, sy+60} )
			if fx~=-1 and fy~=-1 then 
				maps[y][x] = 2
			end
			local fx,fy = findMultiColorInRegionFuzzyByTableQin(tbColor3, {sx, sy, sx+60, sy+60} )
			if fx~=-1 and fy~=-1 then 
				maps[y][x] = 3
			end
			local fx,fy = findMultiColorInRegionFuzzyByTableQin(tbColor4, {sx, sy, sx+60, sy+60} )
			if fx~=-1 and fy~=-1 then 
				maps[y][x] = 4
			end
			local fx,fy = findMultiColorInRegionFuzzyByTableQin(tbColor5, {sx, sy, sx+60, sy+60} )
			if fx~=-1 and fy~=-1 then 
				maps[y][x] = 5
			end
			--local fx,fy = findMultiColorInRegionFuzzyByTableQin(tbColor6, {sx, sy, sx+60, sy+60} )
			local fx,fy = findMultiColorInRegionFuzzyByTable(tbColor6,96, sx, sy, sx+60, sy+60)
			if fx~=-1 and fy~=-1 then 
				maps[y][x] = 6
			end	
		end
		
	end
	keepScreen(false);   --关闭保持屏幕，当参数为 false 时关闭屏幕保持，释放内存中的屏幕图像，以备下一次继续保持。

end
clear()
local str = dump(maps)
--dialog(str, 0)

--local str = dump(coordinateMaps)
--dialog(str, 0)
local function chekCell()
	for y=1,8 do 
		for x=1, 7 do 
			local bDie, dx, dy, action= isDie(maps, x, y)
			if not bDie then 
				local tb = {x,y,bDie, dx, dy, action}
				--dialog(dump(tb))	
				local xy = coordinateMaps[dy][dx]
				click(xy[1],xy[2])
				--dialog(dump(xy))
				mSleep(1000)
				if action=="left" then 
					click(xy[1]-60,xy[2])
				elseif action=="right" then 
					click(xy[1]+60,xy[2])
				elseif action=="up" then 	
					click(xy[1],xy[2]-60)
				elseif action=="down" then 
					click(xy[1],xy[2]+60)
				end				
				return
			end
		end
	end
end

--while(true) do 
--	clear()
--	chekCell()
--	mSleep(2000)

--end

--recognize = ocrText(238, 151, 303, 175, 1);
--mSleep(1000); 
--dialog("识别出的字符："..recognize, 0);

----内容已复制到剪贴板!
--local tab = {
--"33033ff300f031be7d806fcf020c37f0c8787fe301@1$微$83$13$13",
--"0600e03fff000200157caa6553ea9b54caa655f200@0$信$70$13$13",
--}
--local index = addTSOcrDictEx(tab)

--ret = tsOcrText(index,344, 151, 410, 174,"EFF0F0 , 0D0D0D # F0F1F1 , 0E0E0E # F1F2F2 , 0F0F0F # F2F3F3 , 101010 # F3F4F4 , 111111 # F0F0F1 , 131212",90)
--toast(ret)

----请自行更改参数
----1: 0,0,0,0 范围坐标，请自行修改
----2: "EFF0F0 , 0D0D0D # F0F1F1 , 0E0E0E # F1F2F2 , 0F0F0F # F2F3F3 , 101010 # F3F4F4 , 111111 # F0F0F1 , 131212" 偏色,多组或单组.请在偏色列表中选择
----3: 90 匹配精度 【0-100】
--x, y = tsFindText(index, "微信",344, 151, 410, 174, "EFF0F0 , 0D0D0D # F0F1F1 , 0E0E0E # F1F2F2 , 0F0F0F # F2F3F3 , 101010 # F3F4F4 , 111111 # F0F0F1 , 131212", 90)
--dialog("识别到的坐标 	x:"..x.." , y:"..y)



